package com.wtb.javatool.service.impl;

import com.wtb.javatool.common.PageBean;
import com.wtb.javatool.dao.ProjectUserMapper;
import com.wtb.javatool.dao.StudentMapper;
import com.wtb.javatool.dao.UserMapper;
import com.wtb.javatool.dto.UserDto;
import com.wtb.javatool.entity.Student;
import com.wtb.javatool.entity.User;
import com.wtb.javatool.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;
    private final ProjectUserMapper projectUserMapper;
    private final StudentMapper studentMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, ProjectUserMapper projectUserMapper, StudentMapper studentMapper) {
        this.userMapper = userMapper;
        this.projectUserMapper = projectUserMapper;
        this.studentMapper = studentMapper;
    }

    /**
     * 根据用户ID、用户名或手机号查询普通项目用户。
     *
     * @param userId   用户ID
     * @param userName 用户名
     * @param phone    手机号
     * @return 查找到的用户列表，如果未找到则返回空列表。
     */
    @Override
    public List<User> findUser(Long userId, String userName, String phone, Integer projectType, Integer userType) {
        if (projectType == 1)
            return userMapper.findNormalUser(userId, userName, phone);
        else {
            if (userType == 1) return userMapper.findManagerUser(userId, userName, phone);
            else if (userType == 2) return null;
            else return userMapper.findStudentUser(userId, userName, phone, null, null, null, null);
        }
    }

    @Override
    public PageBean<UserDto> findClassStudents(Long classId, Long teamId, Long studentId, String userName, Integer haveTeam, Integer currentPage, Integer size, String studentNo, String college, String major, String className) {
        PageBean<UserDto> userDtoPageBean = new PageBean<>();
        userDtoPageBean.setRows(userMapper.findClassStudents(classId, teamId, studentId, userName, haveTeam, (currentPage-1)*size, size, studentNo, college, major, className));
        userDtoPageBean.setTotalCount(userMapper.findClassStudentsCount(classId, teamId, studentId, userName, haveTeam, studentNo, college, major, className));
        return userDtoPageBean;
    }

    @Override
    public void setTeamForStudents(Long classId, Long teamId, List<Long> studentIds) {
        userMapper.setTeamForStudents(classId, teamId, studentIds);
    }

    @Override
    public PageBean<Student> findAllStudents(Long classId,
                                             Long studentId,
                                             String studentName,
                                             Integer currentPage,
                                             Integer size,
                                             String studentNo,
                                             String college,
                                             String major,
                                             String className) {
        PageBean<Student> studentPageBean = new PageBean<>();
        
        // 优先使用UserMapper查询，保持与原有逻辑一致
        List<Student> students = userMapper.findAllStudents(studentId, studentName, classId, (currentPage-1)*size, size, studentNo, college, major, className);
        Integer totalCount = userMapper.findAllStudentsCount(studentId, studentName, classId, studentNo, college, major, className);
        
        // 如果UserMapper无法获取数据（例如返回为空），则使用StudentMapper尝试
        if ((students == null || students.isEmpty()) && studentMapper != null) {
            try {
                students = studentMapper.findStudents(studentId, studentName, studentNo, college, major, className, (currentPage-1)*size, size);
                totalCount = studentMapper.findStudentsCount(studentId, studentName, studentNo, college, major, className);
                System.out.println("使用StudentMapper获取学生数据: " + (students != null ? students.size() : 0) + "条");
            } catch (Exception e) {
                System.err.println("StudentMapper调用异常: " + e.getMessage());
                e.printStackTrace();
            }
        }
        
        studentPageBean.setRows(students);
        studentPageBean.setTotalCount(totalCount);
        return studentPageBean;
    }
    
    /**
     * 根据小组ID查询小组成员
     *
     * @param teamId 小组ID
     * @return 小组成员列表
     */
    @Override
    public List<UserDto> findStudentsByTeamId(Long teamId) {
        return projectUserMapper.findStudentsByTeamId(teamId);
    }
}
